﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Npgsql;
using LayerData.DataBaseManager;
using LayerEntity.Sistema;
using LayerEntity.Caja;

namespace LayerData.Caja
{
    public    class DCaja
    {
        
        #region Variables
        readonly string mensajeExecuteReader = "Error al Recuperar #replace, revise lo Siguiente: ";
        readonly IPostgreSqlConnectionInfo connectionInfo;
        public IPostgreSqlConnectionInfo CurrentConection { get; set; }
        #endregion
        #region Contructores
        public DCaja()
        {
            this.connectionInfo = ObjectFactory.CreateConnectionInfo();
        }

        public DCaja(IPostgreSqlConnectionInfo con)
        {
            this.connectionInfo = con;
            this.CurrentConection = con;
        }

        #endregion


        #region METODOS MANTENIMIENTOS
        public string MantenimientoCajas(ECaja caja, EnumOperaciones operacion)
        {
            String proc = "";
            String msg = "";
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                proc = (string)PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CAJA.ISP_GRABAR_CAJA", caja.ToObject((int)operacion));
                if (proc == "0")
                {
                    Console.Write("--- TRANZACCION HACEPTADA " + msg);
                    res = true;
                }
                else
                {
                    if (proc == "-1")
                    {
                        res = false;
                        this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                    }
                    else
                    {
                        if (proc == "-2")
                        {
                            res = false;
                            this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                        }
                        else
                        {
                            if (proc == "-3")
                            {
                                res = false;
                                this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                            }
                            else
                            {
                                if (proc == "-4")
                                {
                                    res = false;
                                    this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                                }
                                else
                                {
                                    if (proc == "-5")
                                    {
                                        res = false;
                                        this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());

                                    }
                                    else
                                    {
                                        res = false;
                                        this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                                    }
                                }

                            }
                        }

                    }

                }
                if (res == true)
                {

                    this.CurrentConection.CommintTransaction(tran);
                }
            }
            catch (NpgsqlException ex)
            {
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(CurrentConection.GetPostgreSqlConnection());
                proc = ex.Message;
                throw;

            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return proc;
        }
        public object[] AnulaMovCaja(EMovientoCaja Emov, EnumOperaciones operacion)
        {
            object obj;
            string[] result = null;
            bool res = false;
            NpgsqlConnection con = null;
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                NpgsqlTransaction tran = this.CurrentConection.GetTransaction(con);
                obj = PostgreSqlHelper.ExecuteScalar(this.CurrentConection, tran, "CAJA.ISP_ANULAR_MOV_CAJA", Emov.ToAnulaMovCaja((int)operacion));

                if (obj != null)
                {
                    result = (string[])obj;
                }
                else
                {
                    result = new string[] { "MD-00" };
                }
                if (result[0] == "0")
                {
                    res = true;
                }
                else
                {
                    this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                    res = false;
                }
                if (res == true)
                {
                    this.CurrentConection.CommintTransaction(tran);
                }
            }
            catch (Exception ex)
            {
                result[0] = ex.Message;
                Console.Write(ex.Message);
                this.CurrentConection.CancelarConexionTrans(this.CurrentConection.GetPostgreSqlConnection());
                result = new string[] { ex.Message };
                throw ex;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);

            }
            return result;
        }
        #endregion
        
        #region LISTADO

        public DataTable GetListaTipCaja(ESistema obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;

            AccesoBD db = new AccesoBD();
            try
            {
                con = CurrentConection.GetPostgreSqlConnection();
                dtb = db.EjecutaModelView("caja.v_listar_tipcaja", new object[] { obj.Condicion }, con);
            }
            catch (NpgsqlException ex)
            {
                CurrentConection.CloseConnection(con);
                Console.Write(ex.Message + "/" + ex.Source);
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Los Productos"), ex);
                return null;
            }
            finally
            {
                CurrentConection.CloseConnection(con);
            }
            return dtb;
        }
        public DataTable GetListaCaja(ECaja obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;

            AccesoBD db = new AccesoBD();
            try
            {
                con = CurrentConection.GetPostgreSqlConnection();
                dtb = db.EjecutaModelView("caja.v_listar_cajas", new object[] { obj.Condicion }, con);
            }
            catch (NpgsqlException ex)
            {
                CurrentConection.CloseConnection(con);
                Console.Write(ex.Message + "/" + ex.Source);
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Los Productos"), ex);
                return null;
            }
            finally
            {
                CurrentConection.CloseConnection(con);
            }
            return dtb;
        }

        public List<ECaja> GetBancos() {
            List<ECaja> list = new List<ECaja>();
            NpgsqlDataReader dr = null;
            try
            {
                dr = PostgreSqlHelper.ExecuteReader(connectionInfo, CommandType.Text, "select * from caja.tb_caja_banco where id_tipcaja=3");
                if (dr != null) {
                    if (dr.HasRows) {
                        while (dr.Read()) {
                            list.Add(new ECaja(dr));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Caja y Banco"), ex);
            }
            finally {
                DataBaseUtils.CloseNPgsqlDataReader(dr);
            }
            return list;
        }

        public DataTable getMovPagos(ESistema obj)
        {
            DataTable dtb;
            NpgsqlConnection con = null;

            AccesoBD db = new AccesoBD();
            try
            {
                con = this.CurrentConection.GetPostgreSqlConnection();
                dtb = db.EjecutaModelView("caja.v_listar_doc_caja", new object[] { obj.Condicion }, con);
            }
            catch (NpgsqlException ex)
            {
                this.CurrentConection.CloseConnection(con);
                Console.Write(string.Format("{0}/{1}", ex.Message, ex.Source));
                DataBaseUtils.PrintMessajeError(this.mensajeExecuteReader.Replace("#replace", " Los Productos"), ex);
                return null;
            }
            finally
            {
                this.CurrentConection.CloseConnection(con);
            }
            return dtb;
        }
        #endregion
    }
}
